package com.jason.order.service;

import com.jason.order.dao.TccOrderMapper;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OrderTCCService {
    @Resource
    TccOrderMapper tccOrderMapper;

    /**
     *

    @TwoPhaseBusinessAction(name = "buyProduct",commitMethod = "commit",rollbackMethod = "rollback")
    public int prepare(BusinessActionContext  businessActionContext,
                       @BusinessActionContextParameter(paramName = "id") Integer id,
                          @BusinessActionContextParameter(paramName = "status") Integer status){
        tccOrderMapper.updateStatusById(id,status);
        return 1;
    }
                          **/

    public int prepare( Integer id, Integer status){
        tccOrderMapper.updateStatusById(id,status);
        return 1;
    }

    public boolean commit(BusinessActionContext businessActionContext){
        Integer id = businessActionContext.getActionContext("id",Integer.class);
        Integer status = businessActionContext.getActionContext("status",Integer.class);
        tccOrderMapper.updateStatusById(id,status);
        return true;
    }

    public boolean rollback(BusinessActionContext businessActionContext){
        Integer id = businessActionContext.getActionContext("id",Integer.class);
        Integer status = businessActionContext.getActionContext("status",Integer.class);
        tccOrderMapper.updateStatusById(id,-status);
        return true;
    }
}
